過去談過 Deployment、ReplicaSet 所創建的 Pod 皆屬於 stateless 的情況。
但如要建立 stateful 特性的服務資源物件,可以透過 StatefulSet 完成建立。
對於想要實現下列服務目的者,使用 StatefulSet 是最恰當的~
以上,除了服務 stateful & stateless 重要差異之外,
特別凸顯了 Stable 穩定、Ordered 有序的字眼,這就是與 Deployment 之間最大差別。
配置中分三項說明:
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx # has to match .spec.template.metadata.labels
serviceName: "nginx"
replicas: 3 # by default is 1
template:
metadata:
labels:
app: nginx # has to match .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: k8s.gcr.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "my-storage-class"
resources:
requests:
storage: 1Gi
參考書籍\文章出處:
Kubernetes 建置與執行 書中 P.180~181 內容。
https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/
https://jimmysong.io/kubernetes-handbook/concepts/statefulset.html
https://feisky.gitbooks.io/kubernetes/concepts/statefulset.html